package com.swanclouds.redfish.managers;

import com.swanclouds.api.managers.DiagnosticService;
import com.swanclouds.core.transport.ClientConstants;
import com.swanclouds.core.transport.HttpResponse;
import com.swanclouds.model.common.ActionResponse;
import com.swanclouds.redfish.internal.BaseManagerService;
import com.swanclouds.redfish.managers.domain.DiagnosticServiceModel;
import com.swanclouds.redfish.managers.domain.VirtualMediaModel;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author 李文强
 * description
 */

public class DiagnosticServiceImpl extends BaseManagerService implements DiagnosticService {
    /**
     * 查询诊断服务
     * @return DiagnosticServiceModel
     */
    @Override
    public DiagnosticServiceModel get() {
        return get(DiagnosticServiceModel.class,uri(ClientConstants.PATH_MANAGERS_DIAGNOSTICSERVICE,getManagerId())).execute();
    }

    /**
     * 触发Kvm故障截屏
     * @return DiagnosticServiceModel
     */
    @Override
    public DiagnosticServiceModel captureScreenShot() {
       return post(DiagnosticServiceModel.class,uri(ClientConstants.PATH_MANAGERS_DIAGNOSTICSERVICE_CAPTURESCREENSHOT,getManagerId())).execute();
    }

    /**
     * 删除Kvm故障截屏
     * @return DiagnosticServiceModel
     */
    @Override
    public DiagnosticServiceModel deleteScreenShot() {
        return post(DiagnosticServiceModel.class,uri(ClientConstants.PATH_MANAGERS_DIAGNOSTICSERVICE_DELETESCREENSHOT,getManagerId())).execute();
    }

    /**
     *  下载kvm故障截屏
     * @return ActionResponse
     */
    @Override
    public ActionResponse downloadScreenShot() {
        HttpResponse response = post(Void.class, uri(ClientConstants.PATH_MANAGERS_DIAGNOSTICSERVICE_DOWNLOADSCREENSHOT, getManagerId()))
                .header(ClientConstants.HEADER_CONTENT_TYPE, ClientConstants.CONTENT_TYPE_JSON_CHARSET)
                .executeWithResponse();
        if (response.getStatus() < 400) {
            InputStream inputStream = response.getInputStream();
            OutputStream outputStream;
            try {
                outputStream = new FileOutputStream("");
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
                inputStream.close();
                outputStream.flush();
                outputStream.close();
                return ActionResponse.actionSuccess();
            } catch (Exception e) {
                e.printStackTrace();
                return ActionResponse.actionFailed("Failed to write to file " + e.getMessage(), 400);
            }
        } else {
            return ActionResponse.actionFailed(response.getStatusMessage(),response.getStatus());
        }
    }

    /**
     * 黑盒日志收集
     * @return VirtualMediaModel
     */
    @Override
    public VirtualMediaModel collectBlackBox() {
        return post(VirtualMediaModel.class,uri(ClientConstants.PATH_MANAGERS_DIAGNOSTICSERVICE_COLLECTBLACKBOX,getManagerId())).execute();
    }

    /**
     * 黑盒日志导出
     * @return ActionResponse
     */
    @Override
    public ActionResponse exportBlackBox() {
        HttpResponse response = post(Void.class, uri(ClientConstants.PATH_MANAGERS_DIAGNOSTICSERVICE_EXPORTBLACKBOX, getManagerId()))
                .header(ClientConstants.HEADER_CONTENT_TYPE, ClientConstants.CONTENT_TYPE_JSON_CHARSET)
                .executeWithResponse();
        if (response.getStatus() < 400) {
            InputStream inputStream = response.getInputStream();
            OutputStream outputStream;
            try {
                outputStream = new FileOutputStream("");
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
                inputStream.close();
                outputStream.flush();
                outputStream.close();
                return ActionResponse.actionSuccess();
            } catch (Exception e) {
                e.printStackTrace();
                return ActionResponse.actionFailed("Failed to write to file " + e.getMessage(), 400);
            }
        } else {
            return ActionResponse.actionFailed(response.getStatusMessage(),response.getStatus());
        }
    }

    /**
     * 查询黑盒日志收集状态
     * @return VirtualMediaModel
     */
    @Override
    public VirtualMediaModel getCollectBlackBox() {
        return get(VirtualMediaModel.class,uri(ClientConstants.PATH_TASKSERVICE_TASK_COLLECTBLACKBOX,getManagerId())).execute();
    }
}
